﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using KC.Framework.Base;
using KC.Framework.Tenant;

namespace KC.Service.Core.WebApiService.Business
{
    public abstract class IdSrvOAuth2ClientRequestBase : OAuth2ClientRequestBase
    {
        private const string OAuth2_Authorize_Action = "connect/authorize";
        private const string OAuth2_Token_Action = "connect/token";
        private const string OAuth2_UserInfo_Action = "connect/userinfo";
        private const string OAuth2_JwkSet_Action = ".well-known/openid-configuration/jwks";

        public Tenant Tenant { get; set; }

        #region Api Uri

        /// <summary>
        /// 租户信息Api接口地址：http://admin.kcloudy.com/api/
        ///     本地测试接口地址：http://localhost:1009/api/
        /// </summary>
        protected string AdminApiServiceUrl
        {
            get
            {
                return GlobalConfig.AdminApiUrl;
            }
        }

        /// <summary>
        /// 租户信息Api接口地址：http://cdba.appapi.kcloudy.com/api/
        ///     本地测试接口地址：http://localhost:1106/api/
        /// </summary>
        protected string ApplicationApiUrl
        {
            get
            {
                return GlobalConfig.GetApplicationApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户用户接口地址：http://cTest.accapi.kcloudy.com/api/
        ///     本地测试接口地址：http://localhost:2002/api/
        /// </summary>
        protected string AccoutApiServerUrl
        {
            get
            {
                return GlobalConfig.GetAccountApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户CRM接口地址：http://cTest.crmapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:3002/api/
        /// </summary>
        protected string CrmApiServerUrl
        {
            get
            {
                return GlobalConfig.GetCrmApiUrl(Tenant.TenantName);
            }
        }

        /// <summary>
        /// 租户电商接口地址：http://cTest.shopapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:4001/api/
        /// </summary>
        protected string PortalApiServerUrl
        {
            get
            {
                return GlobalConfig.GetPortalApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户电商Subdomin接口地址：http://subdomin.portalapi.kcloudy.com/api/
        ///     本地测试接口地址：http://subdomin.localhost:4002/api/
        /// </summary>
        protected string PortalSubdomainApiServerUrl
        {
            get
            {
                return GlobalConfig.GetPortalApiUrl(TenantConstant.SubDomain);
            }
        }
        /// <summary>
        /// 租户融资接口地址：http://cTest.marketapi.kcloudy.com/api/
        ///     本地测试接口地址：http://localhost:6002/api/
        /// </summary>
        protected string MarketApiServerUrl
        {
            get
            {
                return GlobalConfig.GetMarketApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户融资Subdomin接口地址：http://subdomin.marketapi.kcloudy.com/api/
        ///     本地测试接口地址：http://subdomin.localhost:5002/api/
        /// </summary>
        protected string MarketSubdomainApiServerUrl
        {
            get
            {
                return GlobalConfig.GetMarketApiUrl(TenantConstant.SubDomain);
            }
        }
        /// <summary>
        /// 租户ERP接口地址：http://cTest.erpapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:6002/api/
        /// </summary>
        protected string ErpApiServerUrl
        {
            get
            {
                return GlobalConfig.GetTrainApiUrl(Tenant.TenantName);
            }
        }

        /// <summary>
        /// 租户WorkFlow接口地址：http://cTest.flowapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:7002/api/
        /// </summary>
        protected string FlowApiServerUrl
        {
            get
            {
                return GlobalConfig.GetFlowApiUrl(Tenant.TenantName);
            }
        }


        /// <summary>
        /// 租户Payment接口地址：http://cTest.Paymentapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:8002/api/
        /// </summary>
        protected string PaymentApiServerUrl
        {
            get
            {
                return GlobalConfig.GetPaymentApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户Weixin接口地址：http://cTest.wxapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:9002/api/
        /// </summary>
        protected string WeixinApiServerUrl
        {
            get
            {
                return GlobalConfig.GetWeixinApiUrl(Tenant.TenantName);
            }
        }

        /// <summary>
        /// 租户SSO接口地址：http://cTest.ssoapi.kcloudy.com/api/
        ///     本地测试接口地址：http://cTest.localhost:1009/api/
        /// </summary>
        protected string SsoApiServerUrl
        {
            get
            {
                return GlobalConfig.GetSsoApiUrl(Tenant.TenantName);
            }
        }
        /// <summary>
        /// 租户subdomin的SSO接口地址：http://subdomin.ssoapi.kcloudy.com/api/
        ///     本地测试接口地址：http://subdomin.localhost:1002/api/
        /// </summary>
        protected string SsoSubdomainApiServerUrl
        {
            get
            {
                return GlobalConfig.GetSsoApiUrl(TenantConstant.SubDomain);
            }
        }

        /// <summary>
        /// 将subdomain（如：subdomain.shopapi.kcloudy.com）的域名替换为tenantName的域名（如：[tenantName].shopapi.kcloudy.com）
        /// 备注：WebApi方法上必须添加特性[Com.WebApi.Core.Attributes.WebApiAllowOtherTenant]，以便其他租户可以访问该接口
        /// </summary>
        /// <param name="subdomain">WebApi的配置地址：subdomain.api.kcloudy.com</param>
        /// <param name="tenantName">接口所有者的TenantName</param>
        /// <returns></returns>
        protected string GetTenantDomain(string subdomain, string tenantName)
        {
            return subdomain.Replace(TenantConstant.SubDomain, tenantName);
        }
        #endregion

        protected IdSrvOAuth2ClientRequestBase(
            Tenant tenant,
            IHttpClientFactory clientFactory,
            Microsoft.Extensions.Logging.ILogger logger)
            : base(clientFactory, logger)
        {
            Tenant = tenant;
        }

        protected override OAuth2ClientInfo GetOAuth2ClientInfo()
        {
            string tenantName = Tenant.TenantName;
            string clientId = TenantConstant.GetClientIdByTenant(Tenant);
            string clientSecret = TenantConstant.GetClientSecretByTenant(Tenant);
            string credential = TenantConstant.Sha256(clientSecret);

            string tokenEndpoint = GlobalConfig.SSOWebDomain + OAuth2_Token_Action;
            //string tokenEndpoint = SSOServerUrl() + OpenIdConnectConstants.OAuth2_Token_Action;
            string grantType = "client_credentials";

            return new OAuth2ClientInfo(tenantName, clientId, clientSecret, credential, tokenEndpoint, grantType);
        }
    }
}
